package com.shop.cereshop.app.service.recommend.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.recommend.CereRecommendDraftDAO;
import com.shop.cereshop.app.dao.recommend.CereRecommendTypeDAO;
import com.shop.cereshop.app.page.recommend.RecommendPageVO;
import com.shop.cereshop.app.service.recommend.CereRecommendDraftService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.domain.recommend.CereRecommendDraft;
import com.shop.cereshop.commons.domain.recommend.CereRecommendType;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【cere_recommend_draft(动态草稿箱表)】的数据库操作Service实现
* @createDate 2025-08-22 16:47:47
*/
@Service
public class CereRecommendDraftServiceImpl extends ServiceImpl<CereRecommendDraftDAO, CereRecommendDraft>
    implements CereRecommendDraftService {

    @Resource
    private CereRecommendDraftDAO cereRecommendDraftDAO;

    @Resource
    private CereRecommendTypeDAO cereRecommendTypeDAO;

    @Override
    public Page<RecommendPageVO> getDraftPage(PageParam param, CereBuyerUser user) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<RecommendPageVO> list = cereRecommendDraftDAO.getDraftPage(user.getBuyerUserId());
        for (RecommendPageVO vo : list) {
            vo.setAtUser(vo.toAtUser());
            vo.init();
            vo.setRecommendId(null);
        }
        PageInfo<RecommendPageVO> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public RecommendPageVO getDraftDetailById(Long draftId) {
        RecommendPageVO vo = cereRecommendDraftDAO.getDraftDetailById(draftId);
        if (vo != null) {
            if (StrUtil.isNotBlank(vo.getRecommendTypeIds())) {
                List<Long> list = Arrays.stream(vo.getRecommendTypeIds()
                                .split(","))
                        .map(String::trim)
                        .map(Long::parseLong)
                        .collect(Collectors.toList());
                List<CereRecommendType> recommendTypeList = Collections.emptyList();
                if (CollUtil.isNotEmpty( list)) {
                    //根据话题id集合获取话题详情列表
                    recommendTypeList = cereRecommendTypeDAO.getRecommendTypeList(list);
                }
                vo.setRecommendTypeList(recommendTypeList);
            }

            //时间转换
            if (vo.getCreateTime() != null) {
                vo.setPublishTimes(TimeUtils.toRelativeTime(String.valueOf(vo.getCreateTime())));
            }
            return vo;
        }
        return null;
    }

    @Override
    public Integer deleteUserInfo(List<Long> recommendIds, Long buyerUserId) {
        Integer i = cereRecommendDraftDAO.deleteUserInfo(recommendIds, buyerUserId);
        return i;
    }
}




